{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "61b72bb9",
   "metadata": {},
   "source": [
    "# Using \"Exponent Format\"\n",
    "\n",
    "The `exponenFormat` parameter in the `theme(...)` function can be used to configure the way \"exponent notation\" looks like on plot.  \n",
    "Available values:\n",
    "- `'e'` for E notation, for example, 1.23e+3, which is the default format.\n",
    "- `'pow'` for superscript power notation.\n",
    "\n",
    "The \"exponent format\" is automatically applied to each value formatted in scientific notation, regardless whether the format is user-defined or chosen automatically based on the data. This format affects every part of a plot, including geoms, scales, labels, and tooltips."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "976dab96",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"EOLHyO\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"library\">\n",
       "       if(!window.letsPlotCallQueue) {\n",
       "           window.letsPlotCallQueue = [];\n",
       "       }; \n",
       "       window.letsPlotCall = function(f) {\n",
       "           window.letsPlotCallQueue.push(f);\n",
       "       };\n",
       "       (function() {\n",
       "           var script = document.createElement(\"script\");\n",
       "           script.type = \"text/javascript\";\n",
       "           script.src = \"https://cdn.jsdelivr.net/gh/JetBrains/lets-plot@v4.2.0/js-package/distr/lets-plot.min.js\";\n",
       "           script.onload = function() {\n",
       "               window.letsPlotCall = function(f) {f();};\n",
       "               window.letsPlotCallQueue.forEach(function(f) {f();});\n",
       "               window.letsPlotCallQueue = [];\n",
       "               \n",
       "               \n",
       "           };\n",
       "           script.onerror = function(event) {\n",
       "               window.letsPlotCall = function(f) {};\n",
       "               window.letsPlotCallQueue = [];\n",
       "               var div = document.createElement(\"div\");\n",
       "               div.style.color = 'darkred';\n",
       "               div.textContent = 'Error loading Lets-Plot JS';\n",
       "               document.getElementById(\"EOLHyO\").appendChild(div);\n",
       "           };\n",
       "           var e = document.getElementById(\"EOLHyO\");\n",
       "           e.appendChild(script);\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%useLatestDescriptors\n",
    "%use lets-plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "077e680b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Lets-Plot Kotlin API v.4.6.0. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.2.0."
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LetsPlot.getInfo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d705bf00",
   "metadata": {},
   "outputs": [],
   "source": [
    "val n = 10\n",
    "val data = mapOf(\n",
    "    \"x\" to (0 until n).toList(),\n",
    "    \"y\" to (0 until n).map { (it + 1 + 0.025 * it) * 10.0.pow(-5) },\n",
    "    \"c\" to (0 until n).map { it * 10.0.pow(10) }\n",
    ")\n",
    "\n",
    "val p = letsPlot(data) { x = \"x\"; y = \"y\"; fill = \"c\" } + geomBar(stat = Stat.identity)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74837cbc",
   "metadata": {},
   "source": [
    "#### 1. E-notation vs. Superscript Power Notation\n",
    "\n",
    "In this example \"scientific notation\" formatting for the guides is chosen automatically, basing on the data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "087acd3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "val powTheme = theme(exponentFormat = \"pow\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a698843f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"3BDgDa\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"layout\":{\n",
       "\"name\":\"grid\",\n",
       "\"ncol\":2,\n",
       "\"nrow\":1,\n",
       "\"fit\":true,\n",
       "\"align\":false\n",
       "},\n",
       "\"figures\":[{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"E-notation (default)\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\",\n",
       "\"fill\":\"c\"\n",
       "},\n",
       "\"data\":{\n",
       "\"c\":[0.0,1.0E10,2.0E10,3.0E10,4.0E10,5.0E10,6.0E10,7.0E10,8.0E10,9.0E10],\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0],\n",
       "\"y\":[1.0E-5,2.025E-5,3.05E-5,4.075000000000001E-5,5.1E-5,6.125000000000001E-5,7.15E-5,8.175000000000001E-5,9.2E-5,1.0225E-4]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"bar\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "},{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"Superscript Power\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\",\n",
       "\"fill\":\"c\"\n",
       "},\n",
       "\"data\":{\n",
       "\"c\":[0.0,1.0E10,2.0E10,3.0E10,4.0E10,5.0E10,6.0E10,7.0E10,8.0E10,9.0E10],\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0],\n",
       "\"y\":[1.0E-5,2.025E-5,3.05E-5,4.075000000000001E-5,5.1E-5,6.125000000000001E-5,7.15E-5,8.175000000000001E-5,9.2E-5,1.0225E-4]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"bar\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"exponent_format\":\"pow\"\n",
       "}\n",
       "}],\n",
       "\"kind\":\"subplots\"\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"3BDgDa\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gggrid(listOf(\n",
    "    p + ggtitle(\"E-notation (default)\"),\n",
    "    p + powTheme + ggtitle(\"Superscript Power\")\n",
    "))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a5b34d7",
   "metadata": {},
   "source": [
    "#### 2. Formatting in `geomText()` and `geomLabel()`\n",
    "\n",
    "By default, values in `geomText()` and `geomLabel()` are always shown in standard notation. \n",
    "\n",
    "Thus by default, the `'pow'` in `theme()` doesn't affect numbers in `geomText()` and `geomLabel()` (see the chart on the left) unless <br>\n",
    "you specify a scientific notation formatting explicitly via the `labelFormat` parameter (see the chart on the right).\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "495ddec1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"M0Zm1b\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"layout\":{\n",
       "\"name\":\"grid\",\n",
       "\"ncol\":2,\n",
       "\"nrow\":1,\n",
       "\"fit\":true,\n",
       "\"align\":false\n",
       "},\n",
       "\"theme\":{\n",
       "\"exponent_format\":\"pow\"\n",
       "},\n",
       "\"figures\":[{\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"bar\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"label\":\"y\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"alpha\":0.8,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"label\",\n",
       "\"fill\":\"white\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\",\n",
       "\"fill\":\"c\"\n",
       "},\n",
       "\"data\":{\n",
       "\"c\":[0.0,1.0E10,2.0E10,3.0E10,4.0E10,5.0E10,6.0E10,7.0E10,8.0E10,9.0E10],\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0],\n",
       "\"y\":[1.0E-5,2.025E-5,3.05E-5,4.075000000000001E-5,5.1E-5,6.125000000000001E-5,7.15E-5,8.175000000000001E-5,9.2E-5,1.0225E-4]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[]\n",
       "},{\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"bar\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"label\":\"y\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"alpha\":0.8,\n",
       "\"label_format\":\".2~e\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"label\",\n",
       "\"fill\":\"white\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\",\n",
       "\"fill\":\"c\"\n",
       "},\n",
       "\"data\":{\n",
       "\"c\":[0.0,1.0E10,2.0E10,3.0E10,4.0E10,5.0E10,6.0E10,7.0E10,8.0E10,9.0E10],\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0],\n",
       "\"y\":[1.0E-5,2.025E-5,3.05E-5,4.075000000000001E-5,5.1E-5,6.125000000000001E-5,7.15E-5,8.175000000000001E-5,9.2E-5,1.0225E-4]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[]\n",
       "}],\n",
       "\"kind\":\"subplots\"\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"M0Zm1b\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val left  = p + geomLabel(alpha = 0.8, fill = \"white\") { label = \"y\" }\n",
    "val right = p + geomLabel(alpha = 0.8, fill = \"white\", labelFormat=\".2~e\") { label = \"y\" }\n",
    "\n",
    "gggrid(listOf(left, right)) + powTheme"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
